//
//  CompanyViewController.m
//  Btpin
//
//  Created by Vic on 2019/3/12.
//  Copyright © 2019 Vic. All rights reserved.
//

#import "CompanyViewController.h"
#import "CQTopBarViewController.h"
#import "LYRecomedController.h"
#import "LYAreaController.h"
#import "LYCompanyController.h"
#import "LYClaimController.h"
#import "LYRecomendCell.h"
#import "CompanyCell.h"
#import "CompanyDetailViewController.h"
//#import "CompanyFilterViewController.h"
#import "CompanyFilterView.h"

//#import "ResetPwdViewController.h"


@interface CompanyViewController ()<UITableViewDelegate, UITableViewDataSource, CQTopBarViewControllerDelegate>
{
    NSMutableArray *itemDataArray;
    
//    NSMutableArray *financeDataArray;
//    NSMutableArray *scaleDataArray;
//    NSMutableArray *tradeDataArray;
    
    NSInteger currentPage;
    
    NSString *district;
    NSString *company_type;
    NSString *keyword;
//    NSString *scale; // 规模
//    NSString *finance; // 融资
//    NSString *trade; // 行业
    
    CompanyFilterView *CFView;
}
//@property (nonatomic, strong) CQTopBarViewController *topBar;
@property (nonatomic, copy) NSString *scale; // 规模
@property (nonatomic, copy) NSString *finance; // 融资
@property (nonatomic, copy) NSString *trade; // 行业
@property (nonatomic, strong) UITableView *companyTableview;
@end

@implementation CompanyViewController

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleDefault;
    [self.navigationController setNavigationBarHidden:NO animated:YES];
}
- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    if (CFView && CFView.filterShowFlag) {
        [CFView dismissView];
    }
}
- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(loadDataAndToTop) name:NotificationForPersonalCompanyRefresh object:nil];
    
    itemDataArray = [NSMutableArray arrayWithCapacity:0];
    currentPage = 1;
    district = @"";
    keyword = @"";
    company_type = @"";
    self.scale = @"";
    self.finance = @"";
    self.trade = @"";
    
//    [self loadLocalData];
    [self initUI];
    [self loadData];
}
- (void)loadDataAndToTop {
    currentPage = 1;
    if (self.companyTableview.contentOffset.y !=0) {
        [self.companyTableview setContentOffset:CGPointMake(0, 0) animated:YES];
    }
    [self loadData];
}
- (void)loadData {
    self.noDataView.hidden = YES;
    SVPHUDShow;
    NSMutableDictionary *dic = [NSMutableDictionary dictionaryWithCapacity:0];
    dic = [UIToolsControl getPublicParameter];
    [dic setObject:district forKey:@"district"];
    [dic setObject:self.scale forKey:@"scale"];
    [dic setObject:company_type forKey:@"company_type"];
    [dic setObject:self.finance forKey:@"finance"];
    [dic setObject:self.trade forKey:@"trade"];
    [dic setObject:keyword forKey:@"keyword"];
    [dic setObject:[NSString stringWithFormat:@"%ld", (long)currentPage] forKey:@"page"];
    [dic setObject:PageSize forKey:@"number"];
    
    [BaseNetManager requestWithMethod:POST path:CompanyGetLists_URL params:dic successBlock:^(NSDictionary *responseObject) {
        if ([responseObject[@"code"] integerValue] == 2000) {
            id data = responseObject[@"data"];
            if ([data isKindOfClass:[NSArray class]]) {
                if (self->currentPage==1) {
                    if (self->itemDataArray.count) {
                        [self->itemDataArray removeAllObjects];
                    }
                }
                for (NSDictionary *dic in data) {
                    CompanyInfoModel *model = [[CompanyInfoModel alloc] init];
                    [model setValuesForKeysWithDictionary:dic];
                    [self->itemDataArray addObject:model];
                }
                self.noDataView.hidden = YES;
                [self.companyTableview.mj_footer endRefreshing];
                [self.companyTableview reloadData];
            }
            if (self->itemDataArray.count) {
            } else {
                self.noDataView.hidden = NO;
                [self.noDataView vcNilStatusType:VCNilStatusTypeOfCommonNoData superView:self.view];
            }
            SVPHUDDismiss;
        } else {
            SVPHUDError(responseObject[@"msg"]);
        }
    } failurBlock:^(NSError *error) {
        SVPHUDError(NetErrorAlertText);
        [self.noDataView vcNilStatusType:VCNilStatusTypeOfNoNet superView:self.view];
    }];
}
- (void)initUI {
    self.view.backgroundColor = RGB(245, 245, 245);
    self.navigationItem.title = @"公司";
    
    UIBarButtonItem *rightItem = [[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"filter_icon"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] style:UIBarButtonItemStyleDone target:self action:@selector(filterTap)];
    self.navigationItem.rightBarButtonItem = rightItem;
    
    __weak typeof(self) weakSelf = self;
    /*
    self.topBar = [[CQTopBarViewController alloc] init];
    self.topBar.sectionTitles = @[@"融资", @"规模", @"行业"];
    self.topBar.delegate = self;
    // 融资
    LYClaimController *financeVC = [[LYClaimController alloc] init];
    financeVC.itemDataArray = financeDataArray;
    financeVC.backBlock = ^(NSArray<PublicCategoryElementModel *> *array) {
        __strong __typeof(weakSelf)strongSelf = weakSelf;
        [strongSelf financeItemTap:array];
    };
    
    // 规模
    LYClaimController *scaleVC = [[LYClaimController alloc] init];
    scaleVC.itemDataArray = scaleDataArray;
    scaleVC.backBlock = ^(NSArray<PublicCategoryElementModel *> *array) {
        __strong __typeof(weakSelf)strongSelf = weakSelf;
        [strongSelf scaleItemTap:array];
    };
    
    // 行业
    LYClaimController *tradeVC = [[LYClaimController alloc] init];
    tradeVC.itemDataArray = tradeDataArray;
    tradeVC.backBlock = ^(NSArray<PublicCategoryElementModel *> *array) {
        __strong __typeof(weakSelf)strongSelf = weakSelf;
        [strongSelf claimItemTap:array];
    };
    
    self.topBar.pageViewClasses = @[financeVC, scaleVC, tradeVC];
    self.topBar.selectSegmentImage = @"pulldown_select";
    self.topBar.segmentImage = @"pulldown_unselect";
    self.topBar.selectedTitleTextColor = THEMECOLOR_OF_ORANGE;
    self.topBar.titleTextColor = THEMECOLOR_OF_TEXT_666666;
    [self addChildViewController:self.topBar];
    [self.view addSubview:self.topBar.view];
    */
    
    self.companyTableview = [UIToolsControl initTableViewWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT-HeightOfTabBar-HeightOfStatusBarAndNavigationBar) target:self style:UITableViewStylePlain];
    self.companyTableview.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:self.companyTableview];
    self.companyTableview.mj_footer = [MJRefreshAutoFooter footerWithRefreshingBlock:^{
        self->currentPage++;
        [weakSelf.companyTableview.mj_footer beginRefreshing];
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(MJDuration * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [weakSelf loadData];
        });
    }];
    self.companyTableview.backgroundColor = COLOR_OF_CLEAR;
    
    CFView = [[CompanyFilterView alloc] initWithFrame:CGRectMake(WIDTH, 0, WIDTH, HEIGHT)];
    [[UIApplication sharedApplication].keyWindow addSubview:CFView];
    CFView.filter = ^(NSString * _Nonnull scale, NSString * _Nonnull finance, NSString * _Nonnull trade) {
        BOOL refresh = NO;
        __strong __typeof(weakSelf)strongSelf = weakSelf;
        if (scale && scale.length && ![strongSelf.scale isEqualToString:scale]) {
            strongSelf.scale = scale;
            refresh = YES;
        }
        if (finance && finance.length && ![strongSelf.finance isEqualToString:finance]) {
            strongSelf.finance = finance;
            refresh = YES;
        }
        if (trade && trade.length && ![strongSelf.trade isEqualToString:trade]) {
            strongSelf.trade = trade;
            refresh = YES;
        }
        if (refresh) {
            self->currentPage = 1;
            [strongSelf loadData];
        }
    };
    
//    [self.topBar.footerView addSubview:self.companyTableview];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return itemDataArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    CompanyCell *cell = [tableView dequeueReusableCellWithIdentifier:isCellID];
    if (!cell) {
        cell = [[CompanyCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:isCellID];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    CompanyInfoModel *model = itemDataArray[indexPath.row];
    [cell reloadDataModel:model];
    
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    CompanyInfoModel *model = itemDataArray[indexPath.row];
    return model.listCellHeight;
}
#pragma mark -- UITableViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    CompanyInfoModel *model = itemDataArray[indexPath.row];
    CompanyDetailViewController *CDVC = [[CompanyDetailViewController alloc] init];
    CDVC.id = model.id;
    CDVC.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:CDVC animated:YES];
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 0.00001;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 0.00001;
}
- (void)dropDownViewTapBack {
    self.noDataView.hidden = YES;
}
- (void)filterTap {
    if (CFView.filterShowFlag) {
        [CFView dismissView];
    } else {
        [CFView showView];
    }
    
    /*
    __weak __typeof(self)weakSelf = self;
    CompanyFilterViewController *CFVC = [[CompanyFilterViewController alloc] init];
    CFVC.filter = ^(NSString * _Nonnull scale, NSString * _Nonnull finance, NSString * _Nonnull trade) {
        BOOL refresh = NO;
        __strong __typeof(weakSelf)strongSelf = weakSelf;
        if (scale && scale.length && ![strongSelf.scale isEqualToString:scale]) {
            strongSelf.scale = scale;
            refresh = YES;
        }
        if (finance && finance.length && ![strongSelf.finance isEqualToString:finance]) {
            strongSelf.finance = finance;
            refresh = YES;
        }
        if (trade && trade.length && ![strongSelf.trade isEqualToString:trade]) {
            strongSelf.trade = trade;
            refresh = YES;
        }
        if (refresh) {
            self->currentPage = 1;
            [strongSelf loadData];
        }
    };
    CFVC.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:CFVC animated:YES];
    */
}
/*
#pragma mark -- Action
- (void)financeItemTap:(NSArray<PublicCategoryElementModel *> *)array {
    [self hiddenNoDataView];
    [self.topBar topBarReplaceObjectsAtIndexes:0 withObjects:@"融资"];
    if (array.count) {
        NSString *str = @"";
        for (PublicCategoryElementModel *model in array) {
            str = [NSString stringWithFormat:@"%@-%@", str, model.name];
            if (finance && (![finance isEqualToString:model.id])) {
                [itemDataArray removeAllObjects];
            }
            finance = model.id;
        }
        if (itemDataArray.count) {
            
        } else {
            currentPage = 1;
            [self loadData];
        }
        NSLog(@"%@", str);
    } else {
        if (finance.length) {
            finance = @"";
            currentPage = 1;
            [self loadData];
        }
    }
}
- (void)scaleItemTap:(NSArray<PublicCategoryElementModel *> *)array {
    [self hiddenNoDataView];
    [self.topBar topBarReplaceObjectsAtIndexes:1 withObjects:@"规模"];
    if (array.count) {
        NSString *str = @"";
        for (PublicCategoryElementModel *model in array) {
            str = [NSString stringWithFormat:@"%@-%@", str, model.name];
            if (scale && (![scale isEqualToString:model.id])) {
                [itemDataArray removeAllObjects];
            }
            scale = model.id;
        }
        if (itemDataArray.count) {
            
        } else {
            currentPage = 1;
            [self loadData];
        }
        NSLog(@"%@", str);
    } else {
        if (scale.length) {
            scale = @"";
            currentPage = 1;
            [self loadData];
        }
    }
}
- (void)claimItemTap:(NSArray<PublicCategoryElementModel *> *)array {
    [self hiddenNoDataView];
    [self.topBar topBarReplaceObjectsAtIndexes:2 withObjects:@"行业"];
    if (array.count) {
        NSString *str = @"";
        for (PublicCategoryElementModel *model in array) {
            str = [NSString stringWithFormat:@"%@-%@", str, model.name];
            if (trade && (![trade isEqualToString:model.id])) {
                [itemDataArray removeAllObjects];
            }
            trade = model.id;
        }
        if (itemDataArray.count) {
            
        } else {
            currentPage = 1;
            [self loadData];
        }
        NSLog(@"%@", str);
    } else {
        if (trade.length) {
            trade = @"";
            currentPage = 1;
            [self loadData];
        }
    }
}
- (void)hiddenNoDataView {
    if (itemDataArray.count) {
        self.noDataView.hidden = YES;
    } else {
        self.noDataView.hidden = NO;
    }
}
- (void)loadLocalData {
    financeDataArray = [NSMutableArray arrayWithCapacity:0];
    scaleDataArray = [NSMutableArray arrayWithCapacity:0];
    tradeDataArray = [NSMutableArray arrayWithCapacity:0];
    
    if ([DataToolsControl sharedManager].allCategoryInfoModel) {
        PublicCategoryElementModel *companyModel1 = [[PublicCategoryElementModel alloc] init];
        companyModel1.name = @"融资";
        companyModel1.id = @"1";
        companyModel1.childArray = [NSMutableArray arrayWithCapacity:0];
        PublicCategoryElementModel *mmodel111 = [[PublicCategoryElementModel alloc] init];
        mmodel111.id = @"0";
        mmodel111.name = @"不限";
        mmodel111.titleName = @"融资";
        [companyModel1.childArray addObject:mmodel111];
        for (NSInteger i = 0; i < [DataToolsControl sharedManager].allCategoryInfoModel.financeArray.count; i++) {
            PublicCategoryElementModel *mmodel1 = [[DataToolsControl sharedManager].allCategoryInfoModel.financeArray[i] copy];
            mmodel1.titleName = @"融资";
            [companyModel1.childArray addObject:mmodel1];
        }
        [financeDataArray addObject:companyModel1];
        
        PublicCategoryElementModel *companyModel2 = [[PublicCategoryElementModel alloc] init];
        companyModel2.name = @"规模";
        companyModel2.id = @"2";
        companyModel2.childArray = [NSMutableArray arrayWithCapacity:0];
        PublicCategoryElementModel *mmodel222 = [[PublicCategoryElementModel alloc] init];
        mmodel222.id = @"0";
        mmodel222.name = @"不限";
        mmodel222.titleName = @"规模";
        [companyModel2.childArray addObject:mmodel222];
        for (NSInteger i = 0; i < [DataToolsControl sharedManager].allCategoryInfoModel.scaleArray.count; i++) {
            PublicCategoryElementModel *mmodel2 = [[DataToolsControl sharedManager].allCategoryInfoModel.scaleArray[i] copy];
            mmodel2.titleName = @"规模";
            [companyModel2.childArray addObject:mmodel2];
        }
        [scaleDataArray addObject:companyModel2];
        
        PublicCategoryElementModel *companyModel3 = [[PublicCategoryElementModel alloc] init];
        companyModel3.name = @"行业";
        companyModel3.id = @"3";
        companyModel3.childArray = [NSMutableArray arrayWithCapacity:0];
        PublicCategoryElementModel *mmodel333 = [[PublicCategoryElementModel alloc] init];
        mmodel333.id = @"0";
        mmodel333.name = @"不限";
        mmodel333.titleName = @"行业";
        [companyModel3.childArray addObject:mmodel333];
        for (NSInteger i = 0; i < [DataToolsControl sharedManager].allCategoryInfoModel.tradeArray.count; i++) {
            PublicCategoryElementModel *mmodel3 = [[DataToolsControl sharedManager].allCategoryInfoModel.tradeArray[i] copy];
            mmodel3.titleName = @"行业";
            [companyModel3.childArray addObject:mmodel3];
        }
        [tradeDataArray addObject:companyModel3];
    }
}
 */

@end
